//
//  HomeBannerView.m
//  DanaKreditLoan
//
//  Created by ZhengChengFeng on 2024/10/30.
//

#import "HomeBannerView.h"

#import "HomeModel.h"

@interface HomeBannerView ()<UIScrollViewDelegate>

@property (nonatomic, strong) UIScrollView *scroll;
@property (nonatomic, strong) UIPageControl *page;
@property (nonatomic, strong) NSTimer *timer;


@end

@implementation HomeBannerView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
    }
    return self;
}

- (void)setBannerList:(NSArray *)bannerList{
   
    _bannerList = bannerList;
   
    [self.timer invalidate];
    
    [self setUpUI];
    
}


- (void)setUpUI{
    
    if (self.scroll) {
        [self.scroll removeFromSuperview];
    }
    
    CGFloat width = self.width;
    
    CGFloat height = self.height;
    
    
    self.scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
    
    [self addSubview:self.scroll];
    

    NSInteger count = self.bannerList.count;
    
    if (count <= 1) {
        
        NSArray *bannerArr = self.bannerList[0];
        
        UIView *wholeView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, width, height)];
        wholeView.backgroundColor = [UIColor clearColor];
        wholeView.userInteractionEnabled  = YES;
        [self.scroll addSubview:wholeView];
        
        CGFloat hh = DimensionBaseIphone6(32);
        
        UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, width, hh)];
        view1.backgroundColor = kTableViewBackColor;
        [wholeView addSubview:view1];
        view1.layer.cornerRadius = hh/2;
        view1.layer.masksToBounds = YES;
        
        UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(0, view1.bottom + DimensionBaseIphone6(8), width, hh)];
        view2.backgroundColor = kTableViewBackColor;
        [wholeView addSubview:view2];
        view2.layer.cornerRadius = hh/2;
        view2.layer.masksToBounds = YES;
        
        UIImageView *ic1 = [[UIImageView alloc] initWithFrame:CGRectMake(2, DimensionBaseIphone6(4), DimensionBaseIphone6(24), DimensionBaseIphone6(24))];
        [view1 addSubview:ic1];
        UILabel *lab1 = [UILabel createLabel:[UIFont fontWithName:kFutura size:11] color:[UIColor colorWithHexString:@"#6C6C6C"]];
        lab1.numberOfLines = 2;
        [view1 addSubview:lab1];
        lab1.frame = CGRectMake(ic1.right + 2, 0, width - ic1.right - 2 - 5, DimensionBaseIphone6(32));

        
        UIImageView *ic2 = [[UIImageView alloc] initWithFrame:CGRectMake(2, DimensionBaseIphone6(4), DimensionBaseIphone6(24), DimensionBaseIphone6(24))];
        [view2 addSubview:ic2];
        UILabel *lab2 = [UILabel createLabel:[UIFont fontWithName:kFutura size:11] color:[UIColor colorWithHexString:@"#6C6C6C"]];
        lab2.numberOfLines = 2;
        [view2 addSubview:lab2];
        lab2.frame = CGRectMake(ic2.right + 2, 0, width - ic2.right - 2 - 5, DimensionBaseIphone6(32));
        
        [ic1 sd_setImageWithURL:[NSURL URLWithString:esString(bannerArr[0])]];
        lab1.text = esString(bannerArr[1]);
        if (bannerArr.count == 4) {
            view2.backgroundColor = kTableViewBackColor;
            [ic2 sd_setImageWithURL:[NSURL URLWithString:esString(bannerArr[2])]];
            lab2.text = esString(bannerArr[3]);
        }
        else{
            view2.backgroundColor = [UIColor clearColor];
        }
       
        return;
    }
    
    for (int i = 0; i < count + 2; i++) {
        CGFloat x = i*width;
        CGFloat y = 0;
        
        UIView *wholeView = [[UIView alloc] initWithFrame:CGRectMake(x, y, width, height)];
        wholeView.backgroundColor = [UIColor clearColor];
        [self.scroll addSubview:wholeView];
        wholeView.userInteractionEnabled  = YES;
        
        NSArray *bannerArr = nil;
        if (i == 0) {
            bannerArr = self.bannerList[count - 1];
        } else if (i == count + 1) {
            bannerArr = self.bannerList[0];
        } else {
            bannerArr = self.bannerList[i - 1];
        }
        
        CGFloat hh = (height - DimensionBaseIphone6(8))/2;
        
        UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, width, hh)];
        view1.backgroundColor = kTableViewBackColor;
        [wholeView addSubview:view1];
        view1.layer.cornerRadius = hh/2;
        view1.layer.masksToBounds = YES;
        
        UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(0, view1.bottom + DimensionBaseIphone6(8), width, hh)];
        view2.backgroundColor = kTableViewBackColor;
        [wholeView addSubview:view2];
        view2.layer.cornerRadius = hh/2;
        view2.layer.masksToBounds = YES;
        
        UIImageView *ic1 = [[UIImageView alloc] initWithFrame:CGRectMake(2, DimensionBaseIphone6(4), DimensionBaseIphone6(24), DimensionBaseIphone6(24))];
        [view1 addSubview:ic1];
        UILabel *lab1 = [UILabel createLabel:[UIFont fontWithName:kFutura size:11] color:[UIColor colorWithHexString:@"#6C6C6C"]];
        lab1.numberOfLines = 2;
        
        lab1.frame = CGRectMake(ic1.right + 2, 0, width - ic1.right - 2 - 5, DimensionBaseIphone6(32));
        
       
        UIImageView *ic2 = [[UIImageView alloc] initWithFrame:CGRectMake(2, DimensionBaseIphone6(4), DimensionBaseIphone6(24), DimensionBaseIphone6(24))];
        
        UILabel *lab2 = [UILabel createLabel:[UIFont fontWithName:kFutura size:11] color:[UIColor colorWithHexString:@"#6C6C6C"]];
        lab2.numberOfLines = 2;
        [view2 addSubview:lab2];
        lab2.frame = CGRectMake(ic2.right + 2, 0, width - ic2.right - 2 - 5, DimensionBaseIphone6(32));
        
        
        [ic1 sd_setImageWithURL:[NSURL URLWithString:esString(bannerArr[0])]];
        lab1.text = esString(bannerArr[1]);
        
        [view1 addSubview:lab1];
        [view2 addSubview:ic2];
        
        if (bannerArr.count == 4) {
            view2.backgroundColor = kTableViewBackColor;
            [ic2 sd_setImageWithURL:[NSURL URLWithString:esString(bannerArr[2])]];
            lab2.text = esString(bannerArr[3]);
        }
        else{
            view2.backgroundColor = [UIColor clearColor];
        }
    }
    

    self.scroll.delegate = self;
    CGPoint contentOffset = CGPointMake(width, 0);
    self.scroll.contentOffset = contentOffset;
    self.scroll.showsHorizontalScrollIndicator = NO;
    self.scroll.showsVerticalScrollIndicator = NO;
    self.scroll.bounces = NO;
    self.scroll.pagingEnabled = YES;
    

    self.scroll.contentSize = CGSizeMake(width*(self.bannerList.count + 2), height);
    

    self.page = [[UIPageControl alloc] init];
    self.page.numberOfPages = self.bannerList.count;
    [self.page setCurrentPageIndicatorTintColor:[UIColor clearColor]];
    
    [self startTimer];
}

- (void)startTimer{
    self.timer = [NSTimer scheduledTimerWithTimeInterval:2.0f target:self selector:@selector(autoScroll) userInfo:nil repeats:YES];
}


- (void)autoScroll{

    NSInteger page = self.page.currentPage;
    page++;

    page = page >= self.bannerList.count ? 0 : page ;
    self.page.currentPage = page;

    [self turnPage];
}


- (void)turnPage{

    CGFloat width = self.scroll.frame.size.width;
    CGFloat height = self.scroll.frame.size.height;
    
    kWeakSelf
    __block CGPoint contentOffset = self.scroll.contentOffset;
    if (self.page.currentPage) {
        [UIView animateWithDuration:0.25f animations:^{
            contentOffset.x = width * (weakSelf.page.currentPage + 1);
            [weakSelf.scroll setContentOffset:contentOffset];
        }];
    } else {

        [UIView animateWithDuration:0.25f animations:^{
            contentOffset.x = width * (weakSelf.bannerList.count + 1);
            [weakSelf.scroll setContentOffset:contentOffset];
        } completion:^(BOOL finished) {
            [weakSelf.scroll scrollRectToVisible:CGRectMake(width, 0, width, height) animated:NO];
        }];
    }
}


- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset{
    [self startTimer];
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    [self.timer invalidate];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    CGFloat width = self.scroll.frame.size.width;
    int page = self.scroll.contentOffset.x / width;
    page = (page == (self.bannerList.count + 1) ? 0 : page - 1);
    self.page.currentPage = page;
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    CGFloat width = self.scroll.frame.size.width;
    CGFloat height = self.scroll.frame.size.height;
    int currentPage = self.scroll.contentOffset.x / width;
    if (currentPage == 0) {

        [self.scroll scrollRectToVisible:CGRectMake(width * self.bannerList.count, 0, width, height) animated:NO];
    } else if (currentPage == (self.bannerList.count + 1)) {

        [self.scroll scrollRectToVisible:CGRectMake(width, 0, width, height) animated:NO];
    }
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
